import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    saveCuringlocatio, //保存
    getCuringlocatioByFid, //详情

} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "showName"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            shelfNum: 1, //养护架数量
            isEdit: false, //按钮权限
            positionData: [], //试件存放位置
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //传fid表示修改 不传表示新增
                fCompanyid: "", //标段id
                fCompanyname: "", //标段名称
                fCuringname: "",
                fCuringno: "",
                fCuringnum: "",
                fCuringnote: "",
                curingList: []
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            fCompanynames: null,
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    // 
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    } // currentRow
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal

                }
            },
            immediate: true,
        },
        showName: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.btnShow = true

        if (this.statusNum == 1) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formData.fCompanyid = this.fCompanyids; //当前登录人id
            this.formData.fCompanyname = this.fCompanynames; //当前登录人名称
            this.isEdit = false
            // this.formObj[1].disabled = false;


        } else if (this.statusNum == 2) { //编辑
            // currentRow
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            }) // currentRow
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.isEdit = false

            // this.formObj[1].disabled = false;



        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.isEdit = true

        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        // 新增养护架架数
        addUnqualifiedInfo() {
            // this.positionData=[]
            var num = this.shelfNum++
            var data = num >= 10 ? num : '0' + num
            this.positionData.push({
                fId: "",
                fCuringno: data + "架",
                fCuringnum: "",
                layerList: []
            });


        },
        addStoreyInfo(index, row) { //新增养护架层数
            this.positionData[index].layerList = []
            for (var i = 1; i <= row.fCuringnum; i++) {
                var data = i >= 10 ? i : '0' + i
                this.positionData[index].layerList.push({
                    fId: "",
                    fCuringno: data + "层"
                })
            }
        },
        // 删除养护架架数
        deleteUnqualifiedInfo(index) {
            this.positionData.splice(index, 1);


        },
        // 删除养护架层数
        deleteStoreyInfo(index) {
            this.positionData[index].fCuringnum--
            this.positionData[index].layerList.splice(this.positionData[index].layerList.length - 1, 1)
        },

        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        // 详情
        getDetail(obj) {
            this.loading = true;
            getCuringlocatioByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClCuringlocation
                    this.formData = data
                    this.positionData = data.curingList
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm() {

          
            this.formData.curingList = this.positionData;
            var total = 0
            this.positionData.map(item => {
                total += (item.fCuringnum - 0)
            })
            this.formData.fCuringnum = total
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data) {
            this.loading = true;

            this.isLock = true
            saveCuringlocatio(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })

        },
    },
};